﻿Imports System.Xml
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.ComponentModel

Public Class frmLoginOptions2
#Region "Load Info"
    Dim ServerConn As ServerConnection
    Dim Sqr As Microsoft.SqlServer.Management.Smo.Server
    Private Sub Database_Load(ByVal UserName As String, ByVal Password As String)
        Try
            Me.Cursor = Cursors.WaitCursor
            ServerConn = New ServerConnection
            ServerConn.LoginSecure = False
            ServerConn.ServerInstance = cbxServer.Text
            ServerConn.Login = UserName
            ServerConn.Password = Password
            Sqr = New Server(ServerConn)
            cbxDatabase.Items.Clear()
            For Each database As Database In Sqr.Databases
                'lstData.Items.Add(database.Name)
                cbxDatabase.Items.Add(database.Name)
            Next
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            ShowError("Thông tin chưa chính xác.Vui lòng kiểm tra lại")
        End Try
    End Sub
#End Region
    Private Sub frmLoginOptions_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.Location = New Point(100, 200)
            Cursor.Current = Cursors.WaitCursor
            Panel_LoadData.Visible = True
            bgWorker.WorkerReportsProgress = True
            bgWorker.WorkerSupportsCancellation = True
            bgWorker.RunWorkerAsync()
        Catch ex As Exception

        End Try
    End Sub
    Private DT As New DataTable
    Private Function Server_Load() As Boolean
        Try
            DT = SmoApplication.EnumAvailableSqlServers(False)
            bgWorker.ReportProgress(2 / (5000 * 100))
            Threading.Thread.Sleep(1)
        Catch ex As Exception
            ShowError()
        End Try
    End Function
    Private Sub bgWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgWorker.DoWork
        e.Result = Server_Load()
    End Sub

    Private Sub bgWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged

    End Sub
    Private Sub bgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
        Try
            cbxServer.ValueMember = "Name"
            cbxServer.DataSource = DT
            Panel_LoadData.Visible = False
            Cursor.Current = Cursors.Default
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmLoginOptions2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Try
            Cursor.Current = Cursors.WaitCursor
            Panel_LoadData.Visible = True
            bgWorker.WorkerReportsProgress = True
            bgWorker.WorkerSupportsCancellation = True
            bgWorker.RunWorkerAsync()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cbxDatabase_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxDatabase.DropDown
        Try
            If txtAccount.Text <> "" And txtPassword.Text <> "" Then
                Database_Load(txtAccount.Text, txtPassword.Text)
                Me.Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cbxOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxOk.Click
        Try
            'write file
            Dim content As String = "Provider=SQLOLEDB;Data Source=" & cbxServer.Text & ";Initial Catalog=" & cbxDatabase.Text & ";User Id=" & txtAccount.Text & ";Password=" & txtPassword.Text & ";"
            Dim CurrentDir As String = IO.Path.GetDirectoryName(Application.ExecutablePath)
            Dim PathConfigFile As String = CurrentDir + "\PathConfig.txt"
            Dim encrypt As New Common
            System.IO.File.WriteAllText(PathConfigFile, encrypt.Encrypt(content))
            Me.Close()
            'generate SQL Connection String fix 04/07/2011 increase performance
            Dim SQLConn As String = "Data Source=" & cbxServer.Text & " ;Initial Catalog=" & cbxDatabase.Text & ";User ID=" & txtAccount.Text & ";Password=" & txtPassword.Text
            PathConfigFile = CurrentDir + "\SQLConn.txt"
            System.IO.File.WriteAllText(PathConfigFile, encrypt.Encrypt(SQLConn))
            Me.Close()
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub

    Private Sub cbxCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxCancel.Click
        Me.Close()
    End Sub
End Class